home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp95 / freyja_t.z / freyja_t / libasm.asm < prev    next >
Assembly Source File  |  1992-04-14  |  9KB  |  605 lines

  1. ; LIBASM.ASM -- Assembly Language DOS Interface Routines
  2. ;    Turbo C/ANSI Version, assumes Small model (64K code, separate 64K data)
  3. ;    Written April 1991 by Craig A. Finseth
  4.  
  5. ; -------------------- Constants and Macros --------------------
  6.  
  7. TRUE    equ    255
  8. FALSE    equ    0
  9. NUL    equ    0
  10. BEL    equ    7
  11. CR    equ    13
  12. LF    equ    10
  13. SPACE    equ    32
  14.  
  15. ; C function call stack layout -- SI, DI, BP, and SP must be preserved
  16.  
  17. old_bp    equ    0
  18. ret_addr equ    2
  19. arg1    equ    4
  20. arg2    equ    6
  21. arg3    equ    8
  22. arg4    equ    10
  23. arg5    equ    12
  24. arg6    equ    14
  25. arg7    equ    16
  26. arg8    equ    18
  27.  
  28. ; "safe" stack layout -- BP is not saved
  29.  
  30. sret_addr equ    0
  31. sarg1    equ    2
  32. sarg2    equ    4
  33. sarg3    equ    6
  34. sarg4    equ    8
  35. sarg5    equ    10
  36. sarg6    equ    12
  37. sarg7    equ    14
  38. sarg8    equ    16
  39.  
  40. clear    macro    x
  41.     xor        x,x
  42.     endm
  43.  
  44. zero    macro    x
  45.     xor        x,x
  46.     endm
  47.  
  48. testz    macro    x
  49.     or        x,x
  50.     endm
  51.  
  52. ; -------------------- Data Segment --------------------
  53.  
  54. _DATA    segment    word public 'DATA'    ; initialized data
  55.  
  56.     extrn    _t_attrib : byte    ; video attribute to use
  57.  
  58. iscolor    db    FALSE
  59. wastext    db    TRUE
  60. _DATA    ends
  61.  
  62. _BSS    segment    word public 'BSS'        ; uninitialized data
  63. oldmode    db    ?
  64. cursor_def dw    ?
  65. _BSS    ends
  66.  
  67. DGROUP    group    _DATA, _BSS
  68.     assume    ds:DGROUP
  69.  
  70. ; -------------------- Code Segment --------------------
  71.  
  72. _TEXT    segment    byte public 'CODE'
  73.     assume    cs:_TEXT
  74.  
  75.     public    _BlockAlloc, _BlockFree, _BlockGet, _BlockPut
  76.     public    _c_service
  77.     public    _JFiniA, _JInitA, _JGetKeyA, _JIsKeyA, _JLightOff, _JLightOn,
  78.     public    _JPushKeyA
  79.     public    _lseeka, _PSystem
  80.     public    _VidInit, _VidFini, _VidBell, _VidChar, _VidClear, _VidCursor
  81.     public    _VidCurOff, _VidCurOn
  82.  
  83. ; ------------------------------------------------------------
  84.  
  85. ; Allocate a block of memory and return the starting paragraph number
  86. ; or zero.
  87.  
  88. ;    unsigned
  89. ;    BlockAlloc(unsigned para)
  90. ;    C Callable
  91.  
  92. _BlockAlloc proc near
  93.     mov    BX,SP
  94.     mov    BX,[BX] + sarg1
  95.     mov    AH,48H
  96.     clc
  97.     int    21H
  98.     jc    alloc_no
  99.     ret
  100. alloc_no: zero    AX
  101.     ret
  102. _BlockAlloc endp
  103.  
  104.  
  105. ; ------------------------------------------------------------
  106.  
  107. ; Free a block of memory.
  108.  
  109. ;    void
  110. ;    BlockFree(unsigned para)
  111. ;    C Callable
  112.  
  113. _BlockFree proc    near
  114.     mov    BX,SP
  115.     mov    BX,[BX] + sarg1
  116.     push    ES
  117.     mov    ES,BX
  118.     mov    AH,49H
  119.     int    21H
  120.     pop    ES
  121.     ret
  122. _BlockFree endp
  123.  
  124.  
  125. ; ------------------------------------------------------------
  126.  
  127. ; Get a block of data from high memory.
  128.  
  129. ;    void
  130. ;    BlockGet(char *to, char huge *from, int len)
  131. ;    C Callable
  132.  
  133. ;    do    {
  134. ;        *to++ = *from++;
  135. ;        } while (--len > 0);
  136.  
  137. _BlockGet proc    near
  138.     push    BP
  139.     mov    BP,SP
  140.     push    DI
  141.     push    SI
  142.     push    DS
  143.     push    ES
  144.  
  145.     mov    DI,[BP] + arg1        ; to
  146.     mov    AX,DS            ;  & segment
  147.     mov    ES,AX
  148.  
  149.     mov    SI,[BP] + arg2        ; from
  150.     mov    AX,[BP] + arg3        ;  & segment
  151.     mov    DS,AX
  152.  
  153.     mov    CX,[BP] + arg4        ; CX is len
  154.     cld
  155.  
  156. ;mloop:    mov    AL,DS:[SI]        ; *from++
  157. ;    inc    SI
  158. ;    mov    ES:[DI],AL        ; *to++ =
  159. ;    inc    DI
  160. ;    loop    mloop            ; while --len>0
  161.  
  162.     rep    movsb
  163.  
  164.     pop    ES
  165.     pop    DS
  166.     pop    SI
  167.     pop    DI
  168.     pop    BP
  169.     ret
  170. _BlockGet endp
  171.  
  172.  
  173. ; ------------------------------------------------------------
  174.  
  175. ; Put a block of data into high memory.
  176.  
  177. ;    void
  178. ;    BlockPut(char huge *to, char *from, int len)
  179. ;    C Callable
  180.  
  181. ;    do    {
  182. ;        *to++ = *from++;
  183. ;        } while (--len > 0);
  184.  
  185. _BlockPut proc    near
  186.     push    BP
  187.     mov    BP,SP
  188.     push    DI
  189.     push    SI
  190.     push    ES
  191.  
  192.     mov    DI,[BP] + arg1        ; to
  193.     mov    AX,[BP] + arg2        ;  & segment
  194.     mov    ES,AX
  195.  
  196.     mov    SI,[BP] + arg3        ; from
  197.  
  198.     mov    CX,[BP] + arg4        ; CX is len
  199.     cld
  200.  
  201. ;mloop:    mov    AL,DS:[SI]        ; *from++
  202. ;    inc    SI
  203. ;    mov    ES:[DI],AL        ; *to++ =
  204. ;    inc    DI
  205. ;    loop    mloop            ; while --len>0
  206.  
  207.     rep    movsb
  208.  
  209.     pop    ES
  210.     pop    SI
  211.     pop    DI
  212.     pop    BP
  213.     ret
  214. _BlockPut endp
  215.  
  216.  
  217. ; ------------------------------------------------------------
  218.  
  219. ; Link to Jaguar special services.
  220.  
  221. _c_service proc near
  222.     push    BP
  223.     mov    BP,SP
  224.     xchg    DI,[BP+4]
  225.     pop    BP
  226.     int    60H
  227.     ret
  228. _c_service endp
  229.  
  230.  
  231. ; ------------------------------------------------------------
  232.  
  233. ; Terminate any special HP95LX processing.
  234.  
  235. ;    void
  236. ;    JFiniA(void)
  237. ;    C Callable
  238.  
  239. _JFiniA    proc    near
  240.     mov    AH,4AH        ; turn on serial port power
  241.     mov    AL,1
  242.     int    15H
  243.     ret
  244. _JFiniA    endp
  245.  
  246.  
  247. ; ------------------------------------------------------------
  248.  
  249. ; Initialize any special HP95LX processing.
  250.  
  251. ;    void
  252. ;    JInitA(void)
  253. ;    C Callable
  254.  
  255. _JInitA    proc    near
  256.     mov    AH,4AH        ; turn off serial port power
  257.     zero    AL
  258.     int    15H
  259.     ret
  260. _JInitA    endp
  261.  
  262.  
  263. ; ------------------------------------------------------------
  264.  
  265. ; Return the next key.  HP95LX specific.
  266.  
  267. ;    int
  268. ;    JGetKeyA(void)
  269. ;    C Callable
  270.  
  271. _JGetKeyA proc    near
  272.     mov    AH,10H
  273.     int    16H
  274.     ret
  275. _JGetKeyA endp
  276.  
  277.  
  278. ; ------------------------------------------------------------
  279.  
  280. ; Return non-zero if a key is available or zero if not.  HP95LX specific.
  281.  
  282. ;    int
  283. ;    JIsKeyA(void)
  284. ;    C Callable
  285.  
  286. _JIsKeyA proc    near
  287.     mov    AH,11H
  288.     int    16H
  289.     jz    jik_none
  290.     mov    AX,1
  291.     ret
  292. jik_none: zero    AX
  293.     ret
  294. _JIsKeyA endp
  295.  
  296.  
  297. ; ------------------------------------------------------------
  298.  
  299. ; Turn off the light sleep mode when checking the key press status.
  300. ; HP95LX specific.
  301.  
  302. ;    void
  303. ;    JLightOff(void)
  304. ;    C Callable
  305.  
  306. _JLightOff proc    near
  307.     mov    AX,4E00H
  308.     int    15H
  309.     ret
  310. _JLightOff endp
  311.  
  312.  
  313. ; ------------------------------------------------------------
  314.  
  315. ; Turn on the light sleep mode when checking the key press status.
  316. ; HP95LX specific.
  317.  
  318. ;    void
  319. ;    JLightOn(void)
  320. ;    C Callable
  321.  
  322. _JLightOn proc    near
  323.     mov    AX,4E01H
  324.     int    15H
  325.     ret
  326. _JLightOn endp
  327.  
  328.  
  329. ; ------------------------------------------------------------
  330.  
  331. ; Push the specified key into the input buffer.  HP95LX specific.
  332.  
  333. ;    void
  334. ;    JPushKeyA(int key)
  335. ;    C Callable
  336.  
  337. _JPushKeyA proc    near
  338.     push    BP
  339.     mov    BP,SP
  340.     mov    CX,[BP] + arg1
  341.     mov    AH,05H
  342.     int    16H
  343.     pop    BP
  344.     ret
  345. _JPushKeyA endp
  346.  
  347.  
  348. ; ------------------------------------------------------------
  349.  
  350. ;    long
  351. ;    lseeka(int fd, long dist, int mode)
  352. ;    C callable
  353.  
  354. _lseeka    proc        near
  355.         mov        BX,SP
  356.         mov        DX,[BX] + sarg2
  357.         mov        CX,[BX] + sarg3
  358.         mov        AX,[BX] + sarg4
  359.         mov        AH,42H
  360.         mov        BX,[BX] + sarg1
  361.         int        21H
  362.         jnc        lseek_a
  363.         mov        DX,-1
  364.         neg        AX
  365. lseek_a:    ret
  366. _lseeka    endp
  367.  
  368.  
  369. ; ------------------------------------------------------------
  370.  
  371. ;    int
  372. ;    PSystem(int AH, ...) /* int DX, int CX, int BX, int AL, int SI, int DI */
  373. ;    C callable
  374. ;    Note:  if the carry flag is set, the return code is negated
  375.  
  376. _PSystem proc    near
  377.     push    BP
  378.     mov    BP,SP
  379.     push    DI
  380.     push    SI
  381.  
  382.     mov    AX,[BP] + arg5        ; AL
  383.     mov    BX,[BP] + arg1        ; AH
  384.     mov    AH,BL
  385.     mov    DX,[BP] + arg2
  386.     mov    CX,[BP] + arg3
  387.     mov    BX,[BP] + arg4
  388.     mov    SI,[BP] + arg6
  389.     mov    DI,[BP] + arg7
  390.  
  391.     clc
  392.     int    21H
  393.     jnc    psyst_ok
  394.     neg    AX
  395.     pop    SI
  396.     pop    DI
  397.     pop    BP
  398.     ret
  399.  
  400. psyst_ok: and    AX,7FFFH
  401.     pop    SI
  402.     pop    DI
  403.     pop    BP
  404.     ret
  405. _PSystem endp
  406.  
  407.  
  408. ; ------------------------------------------------------------
  409.  
  410. ; Initialize the screen to 80x25 text mode.  Used (needed) for direct
  411. ; memory output only.
  412.  
  413. ;    int
  414. ;    VidInit(void)
  415. ;    C callable
  416.  
  417. _VidInit proc    near
  418.     int    11H            ; equipment check
  419.     and    AL,30H
  420.     cmp    AL,30H
  421.     jz    vidi_mret        ; monochome display
  422.  
  423.     mov    iscolor,TRUE
  424.  
  425.     mov    AH,0FH        ; get current video mode
  426.     int    10H
  427.     cmp    AL,2            ; 80x25 BW
  428.     jz    vidi_cret
  429.     cmp    AL,3            ; 80x25 color
  430.     jz    vidi_cret
  431.  
  432.     mov    oldmode,AL    ; save old mode
  433.     mov    wastext,FALSE
  434.     mov    AX,0002H        ; set to mode 2, 80x25 BW
  435.     int    10H
  436.  
  437. vidi_cret:
  438.     mov    CX,0007H
  439.     mov    cursor_def,CX
  440.  
  441.     mov    AX,0B800H    ; color base
  442.     ret
  443.  
  444. vidi_mret:
  445.     mov    CX,000CH
  446.     mov    cursor_def,CX
  447.  
  448.     mov    AX,0B000H    ; monochrome base
  449.     ret
  450. _VidInit endp
  451.  
  452.  
  453. ; ------------------------------------------------------------
  454.  
  455. ; Restore the screen.  Used (needed) for direct memory output only.
  456.  
  457. ;    void
  458. ;    VidFini(void)
  459. ;    C callable
  460.  
  461. _VidFini proc    near
  462.     test    iscolor,0FFH
  463.     jz    vidf_ret
  464.     test    wastext,0FFH
  465.     jnz    vidf_ret
  466.  
  467.     mov    AH,0            ; put old mode back
  468.     mov    AL,oldmode
  469.     int    10H
  470. vidf_ret: ret
  471. _VidFini endp
  472.  
  473.  
  474. ; ------------------------------------------------------------
  475.  
  476. ; Ring the bell.  Usable for both BIOS and direct memory output.
  477.  
  478. ;    void
  479. ;    VidBell(void)
  480. ;    C callable
  481.  
  482. _VidBell proc    near
  483.     mov    AL,BEL
  484.     zero    BL
  485.     mov    AH,14        ; write TTY
  486.     int    10H
  487.     ret
  488. _VidBell endp
  489.  
  490.  
  491. ; ------------------------------------------------------------
  492.  
  493. ; Write the specified character to the current cursor location. 
  494. ; Usable for BIOS output only.
  495.  
  496. ;    void
  497. ;    VidChar(char char)
  498. ;    C callable
  499.  
  500. _VidChar proc    near
  501.     push    BP
  502.     mov    BP,SP
  503.  
  504.     mov    AL,[BP] + arg1
  505.     zero    BH            ; page
  506.     mov    BL,_t_attrib
  507.     mov    CX,1            ; repeat count
  508.  
  509.     mov    AH,9            ; write char and attribute
  510.     int    10H
  511.     pop    BP
  512.     ret
  513. _VidChar endp
  514.  
  515.  
  516. ; ------------------------------------------------------------
  517.  
  518. ; Clear the next COUNT characters.  Usable for BIOS output only.
  519.  
  520. ;    void
  521. ;    VidClear(int count)
  522. ;    C callable
  523.  
  524. _VidClear proc    near
  525.     push    BP
  526.     mov    BP,SP
  527.  
  528.     mov    AL,SPACE
  529.     zero    BH            ; page
  530.     mov    BL,_t_attrib
  531.     mov    CX,[BP] + arg1        ; repeat count: # of char to clear
  532.     test    CX,0FFFFH
  533.     jz    vidc_ret
  534.  
  535.     mov    AH,9            ; write char and attribute
  536.     int    10H
  537. vidc_ret:
  538.     pop    BP
  539.     ret
  540. _VidClear endp
  541.  
  542.  
  543. ; ------------------------------------------------------------
  544.  
  545. ; Set the cursor to the specified row/column.  Usable for both BIOS
  546. ; and direct memory output.
  547.  
  548. ;    void
  549. ;    VidCursor(int row, int col)
  550. ;    C callable
  551.  
  552. _VidCursor proc    near
  553.     push    BP
  554.     mov    BP,SP
  555.  
  556.     mov    CL,[BP] + arg1
  557.     mov    DL,[BP] + arg2        ; col
  558.     mov    DH,CL            ; row
  559.  
  560.     zero    BH            ; page
  561.     mov    AH,2            ; set cursor position
  562.     int    10H
  563.     pop    BP
  564.     ret
  565. _VidCursor endp
  566.  
  567.  
  568. ; ------------------------------------------------------------
  569.  
  570. ; Tur the cursor off.
  571.  
  572. ;    void
  573. ;    VidCurOff(void)
  574. ;    C callable
  575.  
  576. _VidCurOff proc near
  577.     mov    AH,1        ; set cursor size
  578.     mov    CX,0FF00H
  579.     int    10H
  580.     ret
  581. _VidCurOff endp
  582.  
  583.  
  584. ; ------------------------------------------------------------
  585.  
  586. ; Set the cursor to a block cursor.
  587.  
  588. ;    void
  589. ;    VidCurOn(void)
  590. ;    C callable
  591.  
  592. _VidCurOn proc near
  593.     mov    AH,1        ; set cursor size
  594.     mov    CX,cursor_def
  595.     int    10H
  596.     ret
  597. _VidCurOn endp
  598.  
  599.  
  600. _TEXT    ends
  601.  
  602.         end
  603.  
  604. ; end of LIBASM.ASM -- Assembly Language DOS Interface Routines
  605.